package com.sgb.kjwl.view.ui.adapter.shopping;

import android.app.Activity;
import android.text.InputType;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnSelectListener;
import com.sgb.kjwl.model.entity.shopping.ShoppingCarResponse;
import com.sgb.kjwl.pop.InputPop;
import com.sgb.kjwl.utils.KeyboardUtils;
import com.swgk.core.util.UiUtil;
import com.sgb.kjwl.utils.UtilSelectUtils;
import com.swgk.core.event.EventConstant;
import com.sgb.kjwl.view.ui.activity.self.shop.ShopGoodsDetailActivity;
import com.swgk.core.util.MToast;
import com.swgk.core.util.StringUtil;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author:张磊 日期:2021/3/17
 * 说明:采购车item子级
 */
public class ShopChildAdapter extends BaseQuickAdapter<ShoppingCarResponse.ItemsData, BaseViewHolder> {

    public OnSelectListener onSelection;

    public ShopChildAdapter(List<ShoppingCarResponse.ItemsData> beans) {
        super(R.layout.item_child, beans);
    }

    public void setOnSelectListener(OnSelectListener iSelect) {
        onSelection = iSelect;
    }

    @Override
    protected void convert(BaseViewHolder helper, final ShoppingCarResponse.ItemsData item) {
        String goodsPrice = "0.0";
        double lowestBuy = Double.parseDouble(TextUtils.isEmpty(item.getLowestBuy()) ? "0.01" : item.getLowestBuy());
        if (!TextUtils.isEmpty(String.valueOf(item.getPrice()))) {
            goodsPrice = StringUtil.showPrice(String.valueOf(item.getPrice()));
        }
        BigDecimal bd = BigDecimal.valueOf(item.getItemNum());
        bd = bd.setScale(3, BigDecimal.ROUND_HALF_UP);
        helper
                .setImageResource(R.id.iv_select, item.isChecked() ? R.drawable.icon_select_item : R.drawable.icon_no_select_item)
                .setText(R.id.tv_product_name, item.getCartTitle())
                .setText(R.id.tv_price_value, "¥" + goodsPrice + "/" + item.getUnit())
                .setTextColor(R.id.tv_price_value, UiUtil.getColor(item.getPriceType() == 1 ? R.color.new_org : item.getPriceType() == 2 ? R.color.color_FF4B10 : R.color.colorF7AB01))
                .setBackgroundRes(R.id.fl_minus, item.getItemNum() > lowestBuy ? R.drawable.corner2_left_radius : R.drawable.corner2_left_radius_false)
                .setBackgroundRes(R.id.fl_add, item.getItemNum() < 999999.999 ? R.drawable.corner2_right_radius : R.drawable.corner2_right_radius_false)
                .setText(R.id.tv_buy_number, UiUtil.rvZeroAndDot(bd.toString()))
                .setVisible(R.id.sku_tag, item.getPromoteStatus() == 2 && item.getChoosePriceType() != 1)
                .setVisible(R.id.tv_invalid, item.getItemStatus() != 1)
                .setVisible(R.id.icon_huiyuan, item.getChoosePriceType() == 3);

        ImageView ivPhoto = helper.getView(R.id.iv_photo);
        Glide.with(UiUtil.getContext()).load(item.getImageUrls().get(0)).into(ivPhoto);
        TextView tvBuyNumber = helper.getView(R.id.tv_buy_number);
        helper.getView(R.id.iv_select).setVisibility(item.getItemStatus() != 1 ? View.INVISIBLE : View.VISIBLE);
        helper.getView(R.id.rl_action).setVisibility(item.getItemStatus() != 1 ? View.INVISIBLE : View.VISIBLE);

        if (item.getShowCommoditySkuDetail() == null || "".equals(item.getShowCommoditySkuDetail())) {
            helper.setVisible(R.id.tv_material_specifications, false);
        } else {
            helper.setText(R.id.tv_material_specifications, item.getShowCommoditySkuDetail());
        }

        if (item.getItemStatus() == 1) {
            helper.getView(R.id.tv_product_name).setOnClickListener(v -> {
                ShopGoodsDetailActivity.start(mContext, String.valueOf(item.getItemId()), "", item.getSkuId());
            });

            helper.getView(R.id.iv_photo).setOnClickListener(v -> {
                ShopGoodsDetailActivity.start(mContext, String.valueOf(item.getItemId()), "", item.getSkuId());
            });
        } else {
            helper.getView(R.id.tv_product_name).setOnClickListener(null);
            helper.getView(R.id.iv_photo).setOnClickListener(null);
        }
        // 减
        helper.getView(R.id.fl_minus).setOnClickListener(v -> {
            double num = Double.parseDouble(tvBuyNumber.getText().toString());
            if (num > lowestBuy) {
                num = num - 1;
                BigDecimal bd2 = new BigDecimal(num);
                bd2 = bd2.setScale(3, BigDecimal.ROUND_HALF_UP);
                item.setItemNum(num);
                helper.setText(R.id.tv_buy_number, UiUtil.rvZeroAndDot(bd2.toString()));
                helper.getView(R.id.fl_add).setBackgroundResource(R.drawable.corner2_right_radius);
                if (num < lowestBuy) {
                    helper.getView(R.id.fl_minus).setBackgroundResource(R.drawable.corner2_left_radius_false);
                }
                UiUtil.sendMsg(EventConstant.EVENT_UPDATE_PRICES, item);
            } else {
                MToast.showToast(UiUtil.getContext(), "不能低于最低采购量");
            }
        });
        /***
         * 添加
         */
        helper.getView(R.id.fl_add).setOnClickListener(
                v -> {
                    helper.getView(R.id.fl_minus).setBackgroundResource(R.drawable.corner2_left_radius);
                    double num = item.getItemNum();
                    num++;
                    if (num > 999999.999) {
                        MToast.showToast(UiUtil.getContext(), "商品数量不能再添加了");
                        helper.getView(R.id.fl_add).setBackgroundResource(R.drawable.corner2_right_radius_false);
                        return;
                    }
                    item.setItemNum(num);
                    helper.setText(R.id.tv_buy_number, UiUtil.rvZeroAndDot(String.valueOf(item.getItemNum())));
                    UiUtil.sendMsg(EventConstant.EVENT_UPDATE_PRICES, item);
                }
        );
        /***
         * 选择
         */
        helper.getView(R.id.rl_child_btn).setOnClickListener(v -> {
            item.setChecked(!item.isChecked());
            helper.setImageResource(R.id.iv_select, item.isChecked() ? R.drawable.icon_select_item : R.drawable.icon_no_select_item);
//            UiUtil.sendMsg(EventConstant.EVENT_CANCEL_ALL_SELSECT, null);
            onSelection.select(item.isChecked());
        });
        /***
         * 输入弹框
         */
        helper.getView(R.id.tv_buy_number).setOnClickListener(v -> {
            InputPop inputPop = new InputPop(mContext);
            if (!UtilSelectUtils.isDouble3(item.getUnit())) {
                inputPop.mEtInputNumber.setInputType(InputType.TYPE_CLASS_NUMBER);
            }
            inputPop.mEtInputNumber.setText(UiUtil.rvZeroAndDot(item.getItemNum() + ""));
            inputPop.mTvSure.setOnClickListener(v1 -> {
                if (!TextUtils.isEmpty(inputPop.mEtInputNumber.getText().toString())) {
                    double num = Double.parseDouble(inputPop.mEtInputNumber.getText().toString());
                    if (num < lowestBuy) {
                        MToast.showToast(UiUtil.getContext(), "不能低于最低采购量");
                    } else if (num > 999999.999) {
                        MToast.showToast(UiUtil.getContext(), "不能高于最高采购量");
                    } else {
                        item.setItemNum(Double.parseDouble(inputPop.mEtInputNumber.getText().toString()));
                        helper.setText(R.id.tv_buy_number, UiUtil.rvZeroAndDot(String.valueOf(item.getItemNum())));
                        UiUtil.sendMsg(EventConstant.EVENT_UPDATE_PRICES, item);
                        KeyboardUtils.hideSoftInput((Activity) mContext);
                        inputPop.dismiss();
                    }

                } else {
                    inputPop.dismiss();
                }
            });
            inputPop.showPopupWindow();
        });
    }

}
